package com.niit.bc.servlet;

import com.niit.bc.bean.BookBean;
import com.niit.bc.bean.ItemBean;
import com.niit.bc.bean.OrderBean;
import com.niit.bc.bean.UserBean;
import com.niit.bc.biz.BookBiz;
import com.niit.bc.biz.ItemBiz;
import com.niit.bc.biz.OrderBiz;
import com.niit.bc.biz.impl.BookBizImpl;
import com.niit.bc.biz.impl.ItemBizImpl;
import com.niit.bc.biz.impl.OrderBizImpl;
import com.niit.bc.util.web.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class CreateOrder extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public CreateOrder() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        request.setCharacterEncoding("utf8");
        response.setCharacterEncoding("utf8");
        String s_bookId = request.getParameter("bookId");
        int bookId = Integer.parseInt(s_bookId);
        String s_bookCount = request.getParameter("bookCount");
        int bookCount = Integer.parseInt(s_bookCount);
        String orderId = String.valueOf("BC" + new Date().getTime());//生成订单号

        UserBean user = (UserBean) request.getSession().getAttribute(Constants.LOGIN_SESSION_USER);
        int userId = 0;
        if (user != null) {
            userId = user.getUserId();
        }

        if (userId != 0) {
            /*封装*/
            OrderBean orderBean = new OrderBean();
            orderBean.setOrderId(orderId);
            orderBean.setUserId(userId);

            OrderBiz orderBiz = new OrderBizImpl();
            int n = orderBiz.insertOrders(orderBean, userId);
        }

        //向items表中添加数据

        ItemBean itemBean = new ItemBean();
        itemBean.setBookId(bookId);
        itemBean.setOrderId(orderId);
        itemBean.setItemBookNum(bookCount);
        ItemBiz biz = new ItemBizImpl();
        int a = biz.addItems(itemBean);
        BookBean bookBean = new BookBean();
        BookBiz bookBiz = new BookBizImpl();
        List <BookBean> book = bookBiz.findBookByBookId(bookId);
        if (book != null) {
            bookBean = book.get(0);
            double bookAllPrice = bookCount * bookBean.getBookPrice();
            request.setAttribute("bookCount", bookCount);
            request.setAttribute("book", book);
            request.setAttribute("bookAllPrice", bookAllPrice);
            request.setAttribute("orderId", orderId);
            request.getRequestDispatcher("CreateOrder.jsp").forward(request, response);
            return;
        }
    }

    /**
     * The doPost method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
